/**
 * 
 */
package cn.com.shiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.shiro.vo.User;

/**
 * @className 
 * @descripion 
 * @author WangJun
 * @date 2018年6月28日上午11:35:38
 */
@Controller
public class UserController {
	@RequestMapping(value="sublogin",method=RequestMethod.POST,produces = "application/json;charset=utf-8")
	@ResponseBody
	public String sublogin(User user) {
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken  token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
		try {
			token.setRememberMe(user.isRememberMe());
			subject.login(token);
			subject.checkRole("admin");
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
			
		}
		return "登录成功 拥有admin权限";
	}
	
	@RequestMapping(value="testroles",method=RequestMethod.GET)
	@ResponseBody
	public String testRoles() {
		return "test roles success";
	}
	
	@RequestMapping(value="testroles1",method=RequestMethod.GET)
	@ResponseBody
	public String testRoles1() {
		return "test roles1 success";
	}
	@RequiresPermissions("user:delete")
	@RequestMapping(value="testperm",method=RequestMethod.GET)
	@ResponseBody
	public String testperm() {
		return "test perm success";
	}
	@RequiresPermissions("user:delete2")
	@RequestMapping(value="testperm1",method=RequestMethod.GET)
	@ResponseBody
	public String testperm1() {
		return "test perm1 success";
	}
}
